﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.Sale
{
    public partial class 辅料采购清单0 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                if (Request.QueryString["y"] != null)
                {
                    this.NianDu.Text = Request.QueryString["y"].ToString();
                    this.NianDu.Enabled = false;
                    Button_Query_Click(null, null);
                }
                else
                {
                    this.NianDu.Text = DateTime.Now.Year.ToString();
                    this.SqlDataSource1.SelectCommand = "";
                }
                if (Request.QueryString["m"] != null)
                {
                    this.YueFen.Text = Request.QueryString["m"].ToString();
                    this.YueFen.Enabled = false;
                    Button_Query_Click(null, null);
                }
                else
                {
                    this.YueFen.Text = DateTime.Now.Month.ToString();
                    this.SqlDataSource1.SelectCommand = "";
                }


            }
        }

       
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = "select count(*) from 辅料采购计划0 where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text;
            if ((int)Cmd.ExecuteScalar() == 0)
            {
                this.SqlDataSource1.SelectCommand = "";
            }
            else
            {
                string sql = "";
                if (this.NianDu.Text != "0")
                {
                    sql = " where 年度=" + this.NianDu.Text + "and 月份=" + this.YueFen.Text;
                }
                sql = "select 辅料ID,品名,规格,单位,计划购进量,辅料采购计划0.单价,金额"
                    + " from 辅料采购计划0 inner join 材料 on 辅料采购计划0.辅料ID=材料.ID " + sql + " order by 年度 desc,月份 asc";
                this.SqlDataSource1.SelectCommand = sql;
            }
            this.GridView1.SelectedIndex = -1;
        }
        protected void Button_Calc_Click(object sender, EventArgs e)
        {

        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {
                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p + 1;
                    }
                }
                else
                {
                    TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
                }
            }

        }

       

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;

        }

        protected void Button_Export_Click(object sender, EventArgs e)
        {
             string part1 =
@"<html xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:x='urn:schemas-microsoft-com:office:excel'
xmlns='http://www.w3.org/TR/REC-html40'>
<head>
<meta http-equiv=Content-Type content='text/html; charset=utf-8'>
<style>
<!--table
@page
{mso-header-data:;
mso-footer-data:;
margin:.79in .31in .79in .31in;
mso-header-margin:.51in;
mso-footer-margin:.51in;
mso-page-orientation:landscape;
mso-horizontal-page-align:center;
}
-->
</style>
<!--[if gte mso 9]><xml>
 <x:ExcelWorkbook>
  <x:ExcelWorksheets>
   <x:ExcelWorksheet>
    <x:Name>辅料采购清单</x:Name>
    <x:WorksheetOptions>
     <x:DefaultRowHeight>285</x:DefaultRowHeight>
     <x:Print>
      <x:ValidPrinterInfo/>
      <x:Scale>100</x:Scale>
      <x:PaperSizeIndex>9</x:PaperSizeIndex>
      <x:HorizontalResolution>200</x:HorizontalResolution>
      <x:VerticalResolution>200</x:VerticalResolution>
     </x:Print>
     <x:Selected/>
     <x:Panes>
      <x:Pane>
       <x:Number>3</x:Number>
       <x:ActiveRow>0</x:ActiveRow>
       <x:ActiveCol>0</x:ActiveCol>
      </x:Pane>
     </x:Panes>
    </x:WorksheetOptions>
   </x:ExcelWorksheet>
 </x:ExcelWorkbook>
</xml><![endif]-->
</head>
<body>";
            string part2 =
@"<style>
tr{height:30px;}
td{text-align:center;font-size:10pt;}
td.s1{border:.5pt solid #000000;border-top:1pt solid #000000;border-left:1pt solid #000000;}
td.s2{border:.5pt solid #000000;border-top:1pt solid #000000;}
td.s3{border:.5pt solid #000000;border-top:1pt solid #000000;border-right:1pt solid #000000;}
td.s4{border:.5pt solid #000000;border-left:1pt solid #000000;}
td.s5{border:.5pt solid #000000;}
td.s6{border:.5pt solid #000000;border-right:1pt solid #000000;}
td.s7{border:.5pt solid #000000;border-bottom:1pt solid #000000;border-left:1pt solid #000000;}
td.s8{border:.5pt solid #000000;border-bottom:1pt solid #000000;}
td.s9{border:.5pt solid #000000;border-bottom:1pt solid #000000;border-right:1pt solid #000000;}
</style>
<table border=0 cellpadding=0 style=border-collapse:collapse>
<tr>
<td height=60 colspan=11 style=font-size:20pt;><b>&nbsp;辅料采购清单&nbsp;</b></td>
</tr>
</table>";
            string part3 =
@"
<table border=1 cellpadding=0 style=border-collapse:collapse style=font-size:9pt;font-family:@宋体 >
<tr>
<td colspan=2 style=text-align:center;>品名</td>
<td colspan=2 style=text-align:center;>规格</td>
<td colspan=1 style=text-align:center;>计量单位</td>
<td colspan=2 style=text-align:center;>计划购进</td>
<td colspan=2 style=text-align:center;>单价</td>
<td colspan=2 style=text-align:center;>金额</td>
</tr>";
            string part4 =
@"
</table>
</body>
</html>";
       
            Response.Charset = "utf-8";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename=\"" + Server.UrlEncode("辅料采购清单") + ".xls\"");
            Response.Write(part1);
            Response.Write(part2);
            Response.Write(part3);
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = "select count(*) from 辅料采购计划0 where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text;
            string sql = "";
            if ((int)Cmd.ExecuteScalar() != 0)
            {
                if (this.NianDu.Text != "0")
                {
                    sql = " where 年度=" + this.NianDu.Text + "and 月份=" + this.YueFen.Text;
                }
                sql = "select 辅料ID,品名,规格,单位,计划购进量,辅料采购计划0.单价,金额"
                    + " from 辅料采购计划0 inner join 材料 on 辅料采购计划0.辅料ID=材料.ID " + sql + " order by 年度 desc,月份 asc";
            }
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(sql, Conn);
            sda.Fill(ds);
            DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Response.Write("<tr><td colspan=2 style=text-align:center;>"+dt.Rows[i][1].ToString()+"</td>");
                Response.Write("<td colspan=2 style=text-align:center;>" + dt.Rows[i][2].ToString() + "</td>");
                Response.Write("<td colspan=1 style=text-align:center;>" + dt.Rows[i][3].ToString() + "</td>");
                Response.Write("<td colspan=2 style=text-align:center;>" + dt.Rows[i][4].ToString() + "</td>");
                Response.Write("<td colspan=2 style=text-align:center;>" + dt.Rows[i][5].ToString() + "</td>");
                Response.Write("<td colspan=2 style=text-align:center;>" + dt.Rows[i][6].ToString() + "</td></tr>");
            }
            Response.Write(part4);
            Response.End();        
        }

    }
}
